package com.netistate.dao;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.netistate.base.BaseDao;
import com.netistate.pojo.TechNote;

@Repository
public class TechNoteDao extends BaseDao<TechNote, Integer>{

	/**
	 * 查询技术备忘录，不在项目下
	 * @param pid
	 * @param projectProcess
	 * @param begintime
	 * @param endtime
	 *//*
	@SuppressWarnings("unchecked")
	public List<TechNote> findTechNoteByCondition(String pid, String state,
			String level, String reporter, String doer, String begintime,
			String endtime) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		begintime = begintime+" 24:00:00";
		if("现在".equals(endtime)){
	    	  Date date = new Date();
	    	  endtime = df.format(date);
	    }
		endtime = endtime+" 24:00:00";
		
		String hql = "from TechNote where createtime >= '"+begintime+"' and createtime <='" +endtime+"'";
		if("所有".equals(pid)){
			if("所有".equals(state)){
				if("所有".equals(level)){
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "'";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}//reporter
				}else{
					hql = hql + " and level = '"+ level +"'";
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "'";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}//reporter
				}//level
			}else{
				hql = hql + " and state = '" + state + "'";
				if("所有".equals(level)){
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "'";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}//reporter
				}else{
					hql = hql + " and level = '"+ level +"'";
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "'";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}//reporter
				}//level
			}//state
		}else{
			int ppid = Integer.parseInt(pid);
			hql = hql + "and project = '" + ppid + "'";
			if("所有".equals(state)){
				if("所有".equals(level)){
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "'";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}//reporter
				}else{
					hql = hql + " and level ="+ level +"'";
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "'";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}//reporter
				}//level
			}else{
				hql = hql + " and state = '" + state + "'";
				if("所有".equals(level)){
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "'";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}//reporter
				}else{
					hql = hql + " and level = '"+ level +"'";
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "'";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}//reporter
				}//level
			}//state
		}//pid
		
		Query query = getSession().createQuery(hql);
		return query.list();
	}*/
	/**
	 * 查询技术备忘录，不在项目下
	 * @param pid
	 * @param projectProcess
	 * @param begintime
	 * @param endtime
	 */
	@SuppressWarnings("unchecked")
	public List<TechNote> findTechNoteByCondition(String pid, String state,
			String level, String reporter, String doer, String begintime,
			String endtime) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		begintime = begintime+" 24:00:00";
		if("现在".equals(endtime)){
	    	  Date date = new Date();
	    	  endtime = df.format(date);
	    }
		endtime = endtime+" 24:00:00";
		
		String hql = "from TechNote where createtime >= '"+begintime+"' and createtime <='" +endtime+"'";
		if("所有".equals(pid)){
			if("所有".equals(state)){
				if("所有".equals(level)){
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "'";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}//reporter
				}else{
					hql = hql + " and level = '"+ level +"'";
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "'";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}//reporter
				}//level
			}else{
				hql = hql + " and state = '" + state + "'";
				if("所有".equals(level)){
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "' ORDER BY orders ASC";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "' ORDER BY orders ASC";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "' ORDER BY orders ASC";
						}//doer
					}//reporter
				}else{
					hql = hql + " and level = '"+ level +"' ORDER BY orders ASC";
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "' ORDER BY orders ASC";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "' ORDER BY orders ASC";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "' ORDER BY orders ASC";
						}//doer
					}//reporter
				}//level
			}//state
		}else{
			int ppid = Integer.parseInt(pid);
			hql = hql + "and project = '" + ppid + "'";
			if("所有".equals(state)){
				if("所有".equals(level)){
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "'";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}//reporter
				}else{
					hql = hql + " and level ="+ level +"'";
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "'";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "'";
						}//doer
					}//reporter
				}//level
			}else{
				hql = hql + " and state = '" + state + "'";
				if("所有".equals(level)){
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "' ORDER BY orders ASC";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "' ORDER BY orders ASC";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "' ORDER BY orders ASC";
						}//doer
					}//reporter
				}else{
					hql = hql + " and level = '"+ level +"' ORDER BY orders ASC";
					if("所有".equals(reporter)){
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "' ORDER BY orders ASC";
						}//doer
					}else{
						hql = hql + " and reporter = '" + reporter + "' ORDER BY orders ASC";
						if("所有".equals(doer)){
							hql = hql;
						}else{
							hql = hql + " and doer = '" + doer + "' ORDER BY orders ASC";
						}//doer
					}//reporter
				}//level
			}//state
		}//pid
		
		Query query = getSession().createQuery(hql);
		return query.list();
	}

	/**
	 * 根据项目id去查找技术备忘录
	 * @param pid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TechNote> findTechNoteByProjectId(int pid) {
		String hql = "from TechNote where projectid=?";
		Query query = getSession().createQuery(hql);
		query.setParameter(0, pid);
		//query.setInteger(0, pid);
		return query.list();
	}

	/**
	 * 在项目下去有条件的查询技术备忘录
	 * @param state
	 * @param level
	 * @param reporter
	 * @param doer
	 * @param begintime
	 * @param endtime
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TechNote> findProjectTechNoteByCondition(String id,String state,
			String level, String reporter, String doer, String begintime,
			String endtime) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		begintime = begintime+" 24:00:00";
		
		int pid = Integer.parseInt(id);
		
		if("现在".equals(endtime)){
	    	  Date date = new Date();
	    	  endtime = df.format(date);
	    }
		endtime = endtime+" 24:00:00";
		
		String hql = "from TechNote where createtime >= '"+begintime+"' and createtime <='" +endtime+"' and projectid = '" + pid + "'" ;
		if("所有".equals(state)){
			if("所有".equals(level)){
				if("所有".equals(reporter)){
					if("所有".equals(doer)){
						hql = hql;
					}else{
						hql = hql + " and doer = '" + doer + "'";
					}//doer
				}else{
					hql = hql + " and reporter = '" + reporter + "'";
					if("所有".equals(doer)){
						hql = hql;
					}else{
						hql = hql + " and doer = '" + doer + "'";
					}//doer
				}//reporter
			}else{
				hql = hql + " and level = '"+ level +"'";
				if("所有".equals(reporter)){
					if("所有".equals(doer)){
						hql = hql;
					}else{
						hql = hql + " and doer = '" + doer + "'";
					}//doer
				}else{
					hql = hql + " and reporter = '" + reporter + "'";
					if("所有".equals(doer)){
						hql = hql;
					}else{
						hql = hql + " and doer = '" + doer + "'";
					}//doer
				}//reporter
			}//level
		}else{
			hql = hql + " and state = '" + state + "'";
			if("所有".equals(level)){
				if("所有".equals(reporter)){
					if("所有".equals(doer)){
						hql = hql;
					}else{
						hql = hql + " and doer = '" + doer + "'";
					}//doer
				}else{
					hql = hql + " and reporter = '" + reporter + "'";
					if("所有".equals(doer)){
						hql = hql;
					}else{
						hql = hql + " and doer = '" + doer + "'";
					}//doer
				}//reporter
			}else{
				hql = hql + " and level = '"+ level +"'";
				if("所有".equals(reporter)){
					if("所有".equals(doer)){
						hql = hql;
					}else{
						hql = hql + " and doer = '" + doer + "'";
					}//doer
				}else{
					hql = hql + " and reporter = '" + reporter + "'";
					if("所有".equals(doer)){
						hql = hql;
					}else{
						hql = hql + " and doer = '" + doer + "'";
					}//doer
				}//reporter
			}//level
		}//state
		
		Query query = getSession().createQuery(hql);
		return query.list();
	}

	/**
	 * 根据项目名称模糊查询技术备忘录
	 * @param projectName
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TechNote> findTechNoteByProjectName(String projectName) {
		
		String hql = "from TechNote where projectid in (select p.id from Project p where p.projectName like ?)";
		
		Query query = getSession().createQuery(hql);
		query.setParameter(0,  "%"+projectName+"%");
		
		return query.list();
	}

	/**
	 * 根据客户名称模糊查询技术备忘录
	 * @param clientName
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TechNote> findTechNoteByClientName(String clientName) {
		
		String hql = "from TechNote where projectid in (select p.id from Project p where p.clientName like ?)";
		
		Query query = getSession().createQuery(hql);
		query.setParameter(0,  "%"+clientName+"%");
		
		return query.list();
	}

	/**
	 * 按照orders排序，查询30天内的记录
	 * @return
	 */
	public List<TechNote> findAllTechNoteByOrders() {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    calendar.add(Calendar.DATE,-30);
	    Date date2 = calendar.getTime();
		
		String endtime = df.format(date);
		String begintime = df.format(date2);
		
		String hql = "from TechNote where reportDate >= '"+begintime+"' and reportDate <='"+endtime+"' ORDER BY orders ASC";
		Query query = getSession().createQuery(hql);
		
		return query.list();
	}

}
